<?php
class PeopleController extends AppController {
    function login() {
        if ($this->Auth->user('id')) {

            if (!empty($this->data) && $this->data['Person']['remember_me']) {
                $cookie = array();
                $cookie['username'] = $this->data['Person']['username'];
                $cookie['password'] = $this->data['Person']['password'];
                $this->Cookie->write('Auth.User', $cookie, true, '+2 months');
                unset($this->data['Person']['remember_me']);
            
            }
            $this->redirect('/events/index');
        }
        
        if (empty($this->data)) {
            $cookie = $this->Cookie->read('Auth.User');
            if (!is_null($cookie)) {
                if ($this->Auth->login($cookie)) {
                    $this->Session->delete('Message.auth');
                    $this->redirect('/events/index');
                } else {
                    $this->Cookie->delete('Auth.User');
                }
            }
        }
    }

    
    function logout() {
        $this->Cookie->delete('Auth.User');
        $this->Session->destroy();
        $this->redirect($this->Auth->logout());
    }
    
    function isAuthorized() {
        return true;
    }
    
    function beforeRender() {
        parent::beforeRender();
		$this->set('title_for_layout', 'Welcome!');
        $this->data['Person']['password'] = '';
        $this->data['Person']['password_confirm'] = '';
    }
    
    function welcome() {
    }
   
    function adminhelp() {
    }

    function help() {
    }
 
    function register() {
        if (!empty($this->data)) {
            if ($this->data['Person']['password'] == 
                $this->Auth->password($this->data['Person']['password_confirm'])) {
                
                $this->Person->create();
                if ($this->Person->save($this->data)) {
                    $this->Session->setFlash("You have successfully registered.");
                    $this->Auth->login($this->data);
                    $this->redirect('/events/index');
                }
            } else {
                $this->Session->setFlash("Passwords don't match");
            }
        }
        $families = $this->Person->Family->find('list');
        $this->set(compact('families'));
    }
    
    function manage(){
    	$person = $this->Person->find('first', array('conditions' => array('Person.id' => $this->Auth->user('id'))));
    	if(!empty($this->data)){
    		if (($this->data['Person']['password'] == $this->Auth->password("")) && 
    			empty($this->data['Person']['password_confirm'])){
    			$this->data['Person']['password'] = $person['Person']['password'];
    			$this->data['Person']['password_confirm'] = $person['Person']['password'];
    		}else{
    			if($this->Auth->password($this->data['Person']['password_old']) == $person['Person']['password']){
	    			if ($this->data['Person']['password'] != 
		                $this->Auth->password($this->data['Person']['password_confirm'])) {
		                $this->Session->setFlash("Passwords don't match");
		                $this->redirect('/people/manage/');
		            }
    			}else{
    				$this->Session->setFlash("The old password provided is not correct. ");
    				$this->redirect('/people/manage/');
    			}
    		}
    		
   			if($this->Person->save($this->data)){
   				$this->Session->setFlash("You have successfully updated your personal info. ");
   				//session_unset();
   				$this->redirect('/events/index');
   			}else{
   				$this->Session->setFlash("Your update was failed. ");
   			}
    	}
    	$this->set('person', $person);
    }
}

?>
